package org.dromara.hrp.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseService;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hrp.domain.vo.HrpPartnerVo;
import org.dromara.hrp.domain.bo.HrpPartnerBo;
import org.dromara.hrp.service.impl.HrpPartnerServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 合作伙伴
 *
 * @author wenrui
 * @date 2024-03-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hrp/partner")
public class HrpPartnerController extends BaseService {

  private final HrpPartnerServiceImpl hrpPartnerService;

  /**
   * 查询合作伙伴列表
   */
  @SaCheckPermission("hrp:partner:list")
  @GetMapping("/list")
  public TableDataInfo<HrpPartnerVo> list(HrpPartnerBo bo, PageQuery pageQuery) {
    return hrpPartnerService.queryPageList(bo, pageQuery);
  }

  /**
   * 导出合作伙伴列表
   */
  @SaCheckPermission("hrp:partner:export")
  @Log(title = "合作伙伴", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HrpPartnerBo bo, HttpServletResponse response) {
    List<HrpPartnerVo> list = hrpPartnerService.queryList(bo);
    ExcelUtil.exportExcel(list, "合作伙伴", HrpPartnerVo.class, response);
  }

  /**
   * 获取合作伙伴详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("hrp:partner:query")
  @GetMapping("/{id}")
  public R<HrpPartnerVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
    return R.ok(hrpPartnerService.queryById(id));
  }

  /**
   * 新增合作伙伴
   */
  @SaCheckPermission("hrp:partner:add")
  @Log(title = "合作伙伴", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody HrpPartnerBo bo) {
    return toAjax(hrpPartnerService.insertByBo(bo));
  }

  /**
   * 修改合作伙伴
   */
  @SaCheckPermission("hrp:partner:edit")
  @Log(title = "合作伙伴", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrpPartnerBo bo) {
    return toAjax(hrpPartnerService.updateByBo(bo));
  }

  /**
   * 删除合作伙伴
   *
   * @param ids 主键串
   */
  @SaCheckPermission("hrp:partner:remove")
  @Log(title = "合作伙伴", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ids) {
    return toAjax(hrpPartnerService.deleteWithValidByIds(List.of(ids), true));
  }
}
